import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;


public class Lab1 {
    private String site_m; //monkey的位置
    private String site_ba; //banana的位置
    private String site_box;//箱子的位置
    private int on ;  //on值为1时表示猴子在箱子上，on值为0时表示猴子不在箱子上
    private int holds = 0; //holds值为1时表示猴子拿到了香蕉，holds值为0时表示猴子手里为空
    private int step = 0;
    private static int Ax=100,Ay=200;
    private static int Cx=200,Cy=100;
    private static int Bx=300,By=200;
    private static int monkeyX,monkeyY;
    private static int boxX,boxY;
    private static int baX,baY;
    private static List<String>monkeyState=new ArrayList<>();
    private static List<String>boxState=new ArrayList<>();
    private static ImageIcon monkey = new ImageIcon("src/monkey.jpg");
    private static ImageIcon box = new ImageIcon("src/box.png");
    private static ImageIcon banana = new ImageIcon("src/banana.jpg");
    public Lab1() {

    }

    public Lab1(String site_m, String site_ba, String site_box,int on) {
        this.on=on;
        this.site_m = site_m;
        this.site_ba = site_ba;
        this.site_box = site_box;

        switch (site_m){
            case "A":monkeyX=Ax;monkeyY=Ay;break;
            case "B":monkeyX=Bx;monkeyY=By;break;
            case "C":monkeyX=Cx;monkeyY=Cy;break;
        }
        if(on==1){
            monkeyY-=50;
        }
        monkeyState.add(monkeyX+","+monkeyY);
        switch (site_box){
            case "A":boxX=Ax;boxY=Ay;break;
            case "B":boxX=Bx;boxY=By;break;
            case "C":boxX=Cx;boxY=Cy;break;
        }
        boxState.add(boxX+","+boxY);
        switch (site_ba){
            case "A":baX=Ax;baY=Ay;break;
            case "B":baX=Bx;baY=By;break;
            case "C":baX=Cx;baY=Cy;break;
        }


    }

    public void show() {
        System.out.println("当前状态：猴子的位置为" + this.site_m + "香蕉的悬挂位置为" + this.site_ba + "箱子的位置为" + this.site_box);
    }

    public static void main(String[] args) {
        Lab1 lab = new Lab1("A", "C", "B",0);

        lab.show();
        while(lab.holds==0){
            lab.step++;
            System.out.println("步骤"+lab.step+":");
            lab.change();
            lab.show();
        }
        for(String s: lab.monkeyState){
            System.out.print(s+"->");
        }
        System.out.println("---------");
        for(String s: lab.boxState ){
            System.out.print(s+"->");
        }
        new Ball();


    }

    private void change() {
        if (!site_m.equals(site_box)) {
            System.out.println("猴子从" + site_m + "移动到" + site_box);
            String s = boxState.get(step-1);
            monkeyState.add(s);
            boxState.add(s);
            site_m = site_box;
            return;
        }
        if(on==1&&site_box.equals(site_m)&&!site_box.equals(site_ba)){
            on=0;
            System.out.println("猴子爬下了箱子");
            monkeyY+=50;
            String s = boxState.get(step-1);
            monkeyState.add(s);
            boxState.add(s.split(",")[0]+","+monkeyY);
            return;
        }

        if (!site_box.equals(site_ba) && site_m.equals(site_box)) {
            System.out.println("猴子将箱子从" + site_box + "推到" + site_ba);
            site_m = site_box = site_ba;
            String s = baX+","+boxState.get(step-1).split(",")[1] ;
            monkeyState.add(s);
            boxState.add(s);
            return;

        }


        if (on == 0 && site_box.equals(site_ba) && site_m.equals(site_ba)) {
            on = 1;
            System.out.println("猴子爬上了箱子");
            monkeyY-=50;
            String s = boxState.get(step-1);
            monkeyState.add(s.split(",")[0]+","+monkeyY);
            boxState.add(s);
            return;
        }
        if (holds == 0 && on == 1 && site_box.equals(site_ba) && site_m.equals(site_ba)) {
            holds = 1;
            System.out.println("猴子成功拿到香蕉");
            return;
        }

    }
    static   class Ball {
        int step2=0;
        private myCanvas paint = new myCanvas();
        public Ball(){
            Frame f = new Frame();
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            Timer time = new Timer(1000,new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    if(step2<boxState.size()-1){
                        step2++;
                    }

                    paint.repaint();

                }

            });
            time.start();
            f.add(paint);
            f.setBounds(400,100,500,400);
            f.setVisible(true);
        }
        private class myCanvas extends Canvas{
            @Override
            public void paint(Graphics g) {
                g.drawImage(banana.getImage(), baX,baY,50,50,null);
                int monkeyX2=Integer.parseInt(monkeyState.get(step2).split(",")[0]);
                int monkeyY2=Integer.parseInt(monkeyState.get(step2).split(",")[1]) ;
                int boxX2=Integer.parseInt(boxState.get(step2).split(",")[0]);
                int boxY2=Integer.parseInt(boxState.get(step2).split(",")[1]) ;
                if(monkeyX2 == boxX2)
                {
                    monkeyX2 += 50;
                    if(monkeyY2 != boxY2)
                        monkeyX2 -= 50;
                }
                g.drawImage(monkey.getImage(),monkeyX2,monkeyY2, 50,50,null);

                g.drawImage(box.getImage(),boxX2,boxY2,50,50,null);
            }
        }

    }


}




